import { GaugeSurvey, QRActionApi } from '../../api';
import { IGetSurvey } from '../../api/GaugeSurvey';
import {
  IAllGauge,
  IGauge,
  IParentPosition,
  IPosititonInfo,
  ISubject,
  ISubjectShow,
  ISubjectV,
} from '../../assets/js/DataJson';
import { ICacheResult } from './../../Model/Model';
import { ISurvey } from './../../Model/SurveyModel';
import Parser from './../Parser';
import { ISurveyHook } from './SurveyHook';
interface ISubmitSurvey {
  answerJson: string;
  questionAnswerJson: string;
  writeType: string;
  id?: string;
  visitNumber?: number;
  userId?: string;
  channel?: string;
  patientId?: string;
  patient_sex?: number | null;
  questionId: string;
  code?: string;
}
/**
 * 量表私有方法类
 */
class SurveyModelUtil {
  public state = {
    pageCatchTypes: ['surveyUpdate', 'surveyCreate', 'surveyCreateVisit', 'surveyQrcodeUpdate'], // 需要缓存的进入方式
    handleCountData: {
      countData: ['FormSelect', 'FormRadio', 'FormCheckbox'], // 需要处理算分的组件
      countArr: ['FormCheckbox'], // 处理组件里面需要算分的
      countArrKey: {
        // 要处理的具体数据
        FormCheckbox: 'checkboxs',
        // tslint:disable-next-line:no-any
      } as any,
    },
    // tslint:disable-next-line:no-any
    timer: null as any,
  };

  /**
   * 从云端获取初始化对应的数据
   */
  // tslint:disable-next-line:no-any
  public getSurveyData = async (payload: any) => {
    // tslint:disable-next-line:no-any
    let surveyInfo = {} as ICacheResult;
    let gaugeData = {} as IGetSurvey;
    switch (payload.writeType) {
      // 编辑问卷
      case 'surveyUpdate':
        surveyInfo = await this.getCacheGauge(payload.patientId, payload.answerId);
        if (!surveyInfo.answerCacheJson) {
          surveyInfo = await this.getGaugeDetail(payload.answerId);
        }
        break;
      // 查看详情
      case 'surveyDetail':
        surveyInfo = await this.getGaugeDetail(payload.answerId);
        break;
      // 新建病例或者新增问卷
      case 'surveyCreate':
        if (payload.patientId) {
          surveyInfo = await this.getCacheGauge(payload.patientId, payload.answerId);
        } else {
          surveyInfo = await this.getUnknownCacheGauge();
        }
        break;
      // 扫码进入,不缓存
      case 'surveyQrcodeCreate':
        const surveyQrcodeCreate = await this.getQRCodeMsg(payload.code);
        gaugeData = surveyQrcodeCreate.gaugeData;
        surveyInfo = surveyQrcodeCreate.surveyInfo;
        return {
          gaugeData,
          surveyInfo,
        };
      // 扫码进入编辑,需要缓存
      case 'surveyQrcodeUpdate':
        const surveyQrcodeUpdate = await this.getQRCodeMsg(payload.code);
        gaugeData = surveyQrcodeUpdate.gaugeData;
        surveyInfo = surveyQrcodeUpdate.surveyInfo;
        delete surveyInfo.answerJson;
        const qrCodeAnswerOrCache = await this.getQrCodeAnswerOrCache(payload.code);
        surveyInfo.answerCacheJson = qrCodeAnswerOrCache.answerCacheJson;
        return {
          gaugeData,
          surveyInfo,
        };
    }
    gaugeData = await this.getSurveyOriginal({ questionId: surveyInfo.questionId });
    return {
      gaugeData,
      surveyInfo,
    };
  };

  /**
   * 初始化量表model的数据
   * @param gaugeData 初始化获取的json
   * @param roleType 患者还是医生
   * @param isInit 是第一次初始化还是单个初始化一个模块
   */

  public surveyInit = async (
    gaugeData: IAllGauge[],
    // tslint:disable-next-line:no-any
    surveyAnswer: any,
    roleType: 'patient' | 'doctor',
    // tslint:disable-next-line:no-any
    payload: any
  ) => {
    // tslint:disable-next-line:no-any
    const answer = surveyAnswer;
    // tslint:disable-next-line:no-any
    const schema: { [key: string]: any } = {};
    // 算分条件隐藏的对象
    // tslint:disable-next-line:no-any
    const countData = {} as { [key: string]: any };
    gaugeData.forEach((item, menuIndex) => {
      item.isError = [];
      item.position = {};
      let itemNum = 0;
      item.moduleChildrens.forEach((sub: IGauge, menuIChild: number) => {
        sub.subjectShowStatus = {} as ISubjectShow;
        sub.isShow = true;
        let subNum = 0;
        sub.subjects.forEach(async (sje, subjectIndex) => {
          // tslint:disable-next-line:no-any
          sje.subjectData.countNum = {} as { [key: string]: any };
          // 当前填写人是否有权限查看对应的题目
          const isDelete =
            roleType === 'patient'
              ? this.patientVisitIf(sje, payload.visitNumber)
              : this.doctorVisitIf(sje, payload.visitNumber);

          sje.roleShowStatus = isDelete;
          // 以字段作为key记录题目的位置,当前小模块里面的题目显示状态,默认是true
          sub.subjectShowStatus![sje.subjectData.field] = isDelete;
          sje.subjectData.showStatus = isDelete;
          sje.subjectData.defaultValue = surveyAnswer[sje.subjectData.field];

          // 判断是否需要根据权限重新给校验条件赋值
          if (roleType === 'patient' && sje.subjectData.patientRequired === 'true') {
            sje.validatorData.required = sje.subjectData.patientRequired;
          }

          // 这里先暂时单独处理一下
          if (sje.validatorData.pattern) {
            sje.validatorData.pattern = new RegExp(sje.validatorData.pattern);
          }

          if (isDelete) {
            // 以字段作为key记录题目的位置，这里是大模块下全部的题目，以及位置
            item.position[sje.subjectData.field] = {
              menuIndex, // 记录大模块位置
              menuIChild, // 记录小模块位置
              subjectIndex, // 记录题目位置
              errMsg: '', // 错误清空
            };
            // 如果条件隐藏的，有能从缓存答案里面例获取出来值，则默认显示
            if (!sje.subjectData.visibleIf || (sje.subjectData.visibleIf && sje.subjectData.defaultValue)) {
              answer[sje.subjectData.field] = sje.subjectData.defaultValue || null; // 回显
              schema[sje.subjectData.field] = sje.validatorData;
            } else {
              sje.subjectData.showStatus = false;
            }

            // 如果是一个复合组件，且没有默认值的情况下，则初始化他的默认值
            if (sje.subjectData.subjectType === 'FormGroup') {
              if (!sje.subjectData.defaultValue) {
                const { groupItem } = await this.formGroupInit(sje);
                sje.subjectData.defaultValue = [groupItem];
                answer[sje.subjectData.field] = sje.subjectData.defaultValue;
              }
              sje = await this.getGroupPosition(item.position[sje.subjectData.field], sje, roleType);
            }
          } else {
            subNum++;
          }

          // 算分对象的初始化
          countData[sje.subjectData.field] = await this.getCountData(sje.subjectData, true);
        });
        sub.isShow = subNum !== sub.subjects.length;
        if (!sub.isShow) {
          itemNum++;
        }
      });
      item.isShow = itemNum !== item.moduleChildrens.length;
    });
    return {
      answer,
      schema,
      gaugeData,
      menuIndex: this.single(gaugeData),
      writeType: payload.writeType,
      currentCatchIf: this.state.pageCatchTypes.includes(payload.writeType),
      countData,
    };
  };

  /**
   * 判断该问卷为什么类型的提交，并提交到对应接口
   * @params params 要提交的参数
   */
  public submitSurvey = async (params: ISubmitSurvey) => {
    switch (params.writeType) {
      // 编辑问卷
      case 'surveyUpdate':
        await GaugeSurvey.updateGauge({
          answerJson: params.answerJson,
          id: params.id,
          questionId: params.questionId,
        });
        return {};
      // 新增病例
      case 'surveyCreate':
        await GaugeSurvey.createGauge({
          answerJson: params.answerJson,
          visitNumber: params.visitNumber,
          patientId: params.patientId,
          questionId: params.questionId,
        });
        return {};
      // 扫码进入不需要缓存 新建
      case 'surveyQrcodeCreate':
        const surveyQrcodeCreate = await GaugeSurvey.createQrcode({
          answerJson: params.answerJson,
          code: params.code,
        });
        return surveyQrcodeCreate.result;
      // 扫码进入需要缓存 编辑
      case 'surveyQrcodeUpdate':
        const surveyQrcodeUpdate = await GaugeSurvey.updateQrcode({
          answerJson: params.answerJson,
          code: params.code,
        });
        return surveyQrcodeUpdate.result;
    }
  };

  /**
   * 处理Validator的报错
   * @param gaugeData 题目数组
   * @param errors 报错字段以及信息
   */
  // tslint:disable-next-line:no-any
  public catchErrValidator = async (gaugeData: IAllGauge[], errors?: Array<{ field: string; message: string }>) => {
    if (!errors) {
      return gaugeData;
    }
    // 找到报错位置
    const positions = [] as IPosititonInfo[];
    gaugeData.forEach((gaugeItem: IAllGauge, menuIndex: number) => {
      gaugeItem.isError = [];
      errors.forEach((item: { field: string; message: string }) => {
        // 这里由于拿的是所有的报错数据进行循环，所以可能里面会搀着其非当前循环模块东西
        let groupInfo = [] as string[];
        const isGroup = item.field.includes('.');
        let obj = {} as IPosititonInfo;
        // 如果是复合组件则获取里面的内容， 0 复合组件字段 1 小组件下标 2 小组件字段
        if (isGroup) {
          groupInfo = item.field.split('.');
          obj = gaugeItem.position[groupInfo[0]];
          if (obj) {
            obj.fieldChildren = groupInfo[2];
            obj.childrenIndex = Number(groupInfo[1]);
          }
        } else {
          obj = gaugeItem.position[item.field];
        }
        if (obj) {
          obj.errMsg = item.message;
          // 搜集报错信息，这里可以颗粒化到记录出每个模块有多少个报错，目前没有要求暂时不实现
          gaugeItem.isError.push({
            field: isGroup ? groupInfo[2] : item.field,
            menuIndex,
            menuIChild: obj.menuIChild,
          });
          // 🤔 我重新let一个obj 并没有覆盖之前，所以引用的地址一直没变
          positions.push(JSON.parse(JSON.stringify(obj)));
        }
      });
    });

    positions.forEach(item => {
      const subjectData =
        gaugeData[item.menuIndex].moduleChildrens[item.menuIChild].subjects[item.subjectIndex].subjectData;
      // 如果是复合组件, 这里可以改成直接用key取值，留作优化

      if (item.fieldChildren) {
        subjectData.groupChildrens![item.childrenIndex!]!.forEach(group => {
          if (group.subjectData.field === item.fieldChildren) {
            group.subjectData.errMsg = item.errMsg;
            subjectData.errMsg =
              gaugeData[item.menuIndex].moduleChildrens[item.menuIChild].subjects[
                item.subjectIndex
              ].validatorData.message;
          }
        });
      } else {
        subjectData.errMsg = item.errMsg;
      }
    });
    return gaugeData;
  };

  /**
   * 每次答案有改变时就调用
   */
  // tslint:disable-next-line:no-any
  public updateSurveyData = (sje: ISubjectV, schema: any, answer: any, countData: any) => {
    // 显示的时候 我要拿到值和校验信息
    if (sje.subjectData.showStatus) {
      schema[sje.subjectData.field] = sje.validatorData;
    } else {
      answer[sje.subjectData.field] = null;
      delete schema[sje.subjectData.field];
      delete countData[sje.subjectData.field];
    }
    return {
      answer,
      schema,
      countData,
    };
  };

  /**
   * 题目change时调用
   */
  // tslint:disable-next-line:no-any
  public changeSurvey = async (payload: { props: ISurveyHook; val: any }, state: ISurvey) => {
    const field = payload.props.formObj.subjectData.field;
    state.answer[field] = payload.val;
    const position = state.gaugeData[state.menuIndex].position[field];
    if (!position) {
      return null;
    }

    const subject =
      state.gaugeData[state.menuIndex].moduleChildrens[position.menuIChild].subjects[position.subjectIndex];
    subject.subjectData.errMsg = '';
    subject.subjectData.defaultValue = payload.val;
    // 这里单独做一层 算分的处理，这里注意，一定要等到subject.subjectData.defaultValue 赋值完在进行操作😋
    state.countData[payload.props.formObj.subjectData.field] = await this.getCountData(
      payload.props.formObj.subjectData,
      true
    );

    // 取消报错
    state.gaugeData[state.menuIndex].isError = await this.cancelBigModule(state, field);
    const { gaugeData, answer, countData, schema } = await this.visibleIfChange(
      state.gaugeData,
      state.countData,
      state.answer,
      state.schema
    );
    return {
      gaugeData,
      answer,
      countData,
      schema,
    };
  };

  /**
   * 复合组件里面的组件change事件
   */
  // tslint:disable-next-line:no-any
  public changeGroupSurvey = async (payload: { props: ISurveyHook; val: any }, state: ISurvey) => {
    const parentPosition = payload.props.formObj.subjectData.parentPosition!;
    if (!parentPosition) {
      return null;
    }
    const subject =
      state.gaugeData[state.menuIndex].moduleChildrens[parentPosition.menuIChild].subjects[parentPosition.subjectIndex];
    const group = subject.subjectData.groupChildrens!;
    // 这里直接改变复合组件那道题目和复合组件的默认是，这样就不需要再循环取值
    subject.subjectData.defaultValue[parentPosition.groupIndex!][payload.props.formObj.subjectData.field] = payload.val;
    group[parentPosition.groupIndex!]![parentPosition.groupChildrenIndex!].subjectData.defaultValue = payload.val;
    group[parentPosition.groupIndex!]![parentPosition.groupChildrenIndex!].subjectData.errMsg = '';
    subject.subjectData.errMsg = '';
    // 取消报错
    state.gaugeData[state.menuIndex].isError = await this.cancelBigModule(
      state,
      payload.props.formObj.subjectData.field
    );
    state.countData[payload.props.formObj.subjectData.field] = await this.getCountData(subject.subjectData, true);
    // 这里单独做一层 当做传值和回显用
    state.answer[payload.props.formObj.subjectData.field] = subject.subjectData.defaultValue;
    return {
      gaugeData: state.gaugeData,
      answer: state.answer,
      countData: state.countData,
    };
  };

  public errStyle = (data: IAllGauge, index: number, iChild?: number) => {
    if (data.isError) {
      if (iChild || iChild === 0) {
        // tslint:disable-next-line:no-any
        const isErr = data.isError.some((item: any) => {
          return item.menuIndex === index && item.menuIChild === iChild;
        });
        return isErr ? 'err' : '';
      } else {
        // tslint:disable-next-line:no-any
        const isErr = data.isError.some((item: any) => {
          return item.menuIndex === index;
        });
        return isErr ? 'err' : '';
      }
    }
    return '';
  };

  /**
   * 重置dva的state
   */
  public restState = async () => {
    return {
      schema: {},
      gaugeData: [], // 所有量表数据
      answer: {}, // 答案
      doms: [],
      menuIndex: -1, // 1级菜单下标
      menuIChild: -1, // 2级菜单下标
      visitNumber: 0, // 访视次数
      patientId: null, // 患者id
      id: '', // 问卷id
      isDetail: '',
      userId: undefined,
      channel: 0,
      writeType: '',
      currentCatchIf: false,
      questionId: '', // 新建问卷id
      code: '',
    };
  };

  /**
   * 获取复合组件初始化的groupChildrens的默认值
   */
  public formGroupValue = async (data: ISubject) => {
    // tslint:disable-next-line:no-any
    const defaultValue = {} as { [key: string]: any };
    data.initGroupChildrens!.forEach(item => {
      // 复合组件至少有一个所以这里直接去值
      defaultValue[item.subjectData.field] = null;
    });
    return defaultValue;
  };
  /**
   * 定位复合组件 组 的位置
   * @param props 当前题目的信息
   * @param position 当前位置信息
   * @param roleType 填写人权限 判断是否需要重新给校验条件赋值
   */
  public getGroupPosition = async (position: IParentPosition, props: ISubjectV, roleType: 'patient' | 'doctor') => {
    props.subjectData.groupChildrens = [];
    // tslint:disable-next-line:no-any
    props.validatorData.fields = {} as { [key: number]: any };
    props.validatorData.type = 'array';
    // tslint:disable-next-line:no-any
    props.subjectData.defaultValue.forEach((defauVal: { [key: string]: any }, groupIndex: number) => {
      const initGroupChildrens = JSON.parse(JSON.stringify(props.subjectData.initGroupChildrens));
      // tslint:disable-next-line:no-any
      const fields = {} as { [key: string]: any };
      initGroupChildrens.forEach((ori: ISubjectV, groupChildrenIndex: number) => {
        ori.subjectData.defaultValue = defauVal[ori.subjectData.field];
        // 判断是否需要根据权限重新给校验条件赋值
        if (roleType === 'patient' && ori.subjectData.patientRequired === 'true') {
          ori.validatorData.required = ori.subjectData.patientRequired;
        }
        // 这里临时判断条件隐藏
        ori.subjectData.isGroupChildren = true;
        ori.subjectData.parentPosition = {
          ...position,
          groupChildrenIndex,
          groupIndex,
        };
        // 组合验证信息
        fields[ori.subjectData.field] = ori.validatorData;
        props.validatorData.fields[groupIndex] = {
          type: 'object',
          required: ori.validatorData.required,
          fields,
        };
      });
      props.subjectData.groupChildrens!.push(initGroupChildrens);
    });
    return props;
  };

  /**
   * 扫码进入获取相关信息
   * @param code
   */
  public getQRCodeMsg = async (code: string) => {
    const { result } = await QRActionApi.getQRCodeMsg({ code });
    const gaugeData = {
      jsonDesc: result.questionVo.jsonDesc,
    };
    const surveyInfo = {
      visitNumber: result.visitNumber,
      answerJson: result.answerInfoVo.answerJson,
      gender: result.answerInfoVo.gender,
    };
    return { gaugeData, surveyInfo, result };
  };

  /**
   * 条件隐藏，每次答案变化的时候都会触发
   * @param countData 专属算分的对象
   */
  // tslint:disable-next-line:no-any
  public visibleIfChange = async (gaugeData: IAllGauge[], countData: any, answer: any, schema: any) => {
    // tslint:disable-next-line:no-any
    let validatorData = {} as any;
    gaugeData.forEach(gauge => {
      gauge.moduleChildrens.forEach(sub => {
        sub.subjects.forEach(async sje => {
          // 复合权限的在判断 权限 只会在初始化的时候判断一次
          if (sje.roleShowStatus) {
            if (sje.subjectData.visibleIf) {
              try {
                const num = Parser(sje.subjectData.visibleIf, countData, 'visibleIf');
                // tslint:disable-next-line:no-eval
                sje.subjectData.showStatus = !!eval(num);
                if (!sje.subjectData.showStatus) {
                  if (sje.subjectData.subjectType === 'FormGroup') {
                    const { groupItem } = await this.formGroupInit(sje);
                    sje.subjectData.defaultValue = [groupItem];
                    // 这里做临时处理
                    sje.subjectData.groupChildrens = [sje.subjectData.groupChildrens![0]!];
                    sje.subjectData.groupChildrens[0]!.forEach(item => {
                      item.subjectData.showStatus = false;
                      item.subjectData.defaultValue = null;
                    });
                  } else {
                    sje.subjectData.defaultValue = null;
                  }
                }
                // 记录当前模块状态
                sub.subjectShowStatus![sje.subjectData.field] = !!sje.subjectData.showStatus;
                validatorData = await this.updateSurveyData(sje, schema, answer, countData);
              } catch (err) {
                // 隐藏的时候并不会去重新渲染对应的组件而是在render 组件那里直接取消render 所以需要手动重置默认值
                sje.subjectData.showStatus = false;

                // 记录当前模块状态
                sub.subjectShowStatus![sje.subjectData.field] = false;
                if (sje.subjectData.subjectType === 'FormGroup') {
                  const { groupItem } = await this.formGroupInit(sje);
                  sje.subjectData.defaultValue = [groupItem];
                } else {
                  sje.subjectData.defaultValue = null;
                }
              }
            }
            if (sje.subjectData.formula) {
              const formula = Parser(sje.subjectData.formula, countData);
              // 这里直接答案进行赋值，也就是给后端传的
              answer[sje.subjectData.field] = sje.subjectData.defaultValue;
              countData[sje.subjectData.field] = sje.subjectData.defaultValue;
              try {
                // tslint:disable-next-line:no-eval
                sje.subjectData.defaultValue = eval(formula);
              } catch (err) {
                sje.subjectData.defaultValue = 0;
              }
            }
          }
        });
      });
    });
    validatorData = validatorData.answer ? validatorData : { answer, schema };
    gaugeData = this.moduleShowStatus(gaugeData);
    return { gaugeData, countData, ...validatorData };
  };

  /**
   * 节流定时
   */
  // tslint:disable-next-line:no-any
  public delay = (timeout: any) => {
    if (this.state.timer) {
      clearTimeout(this.state.timer);
      this.state.timer = null;
    }
    return new Promise(resolve => {
      this.state.timer = setTimeout(resolve, timeout);
    });
  };

  /**
   * 页面自动定位
   */
  public scrollToWindow = (state: ISurvey) => {
    let range = 0;
    for (let i = 0; i < state.menuIChild; i++) {
      const dom = document.getElementById('gauge_content' + state.menuIndex + '_' + i) as HTMLElement;
      const pdom = dom ? (dom.parentNode as HTMLElement) : null;
      if (pdom) {
        range = range + pdom.scrollHeight + 16;
      }
    }
    if (document.getElementById('scrl_box')) {
      if (!!document.getElementById('scrl_box')!.scrollTo) {
        document.getElementById('scrl_box')!.scrollTo({ behavior: 'smooth', top: range });
        return;
      }
      document.getElementById('scrl_box')!.scrollTop = range;
    }
  };

  /**
   * 判断小模块是否允许显示
   */
  private moduleShowStatus = (gaugeData: IAllGauge[]) => {
    gaugeData.forEach(gauge => {
      gauge.moduleChildrens.forEach(sub => {
        const vals = [];
        // tslint:disable-next-line:forin
        for (const key in sub.subjectShowStatus!) {
          vals.push(sub.subjectShowStatus![key]);
        }
        const isShow = vals.some(item => item);
        sub.isShow = isShow;
      });
    });
    return gaugeData;
  };

  /**
   * 传值和回显
   * 目前 handleCountData 里面的类型是需要特殊的,其他的正常处理
   * @param subjectData 当前题目
   * @param isInit 是否是初始化
   */
  // tslint:disable-next-line:no-any
  private getCountData = async (subjectData: ISubject, isInit: boolean) => {
    const handleCountData = this.state.handleCountData;
    const data = isInit ? subjectData.defaultValue : subjectData[handleCountData.countArrKey[subjectData.subjectType]];
    // 所谓的特殊处理 就是从对应的对象里面 通过传入的值 进行取分数😝
    if (data) {
      // 需要算分的组件
      if (handleCountData.countData.includes(subjectData.subjectType)) {
        if (handleCountData.countArr.includes(subjectData.subjectType)) {
          // tslint:disable-next-line:no-any
          const arr = data.map((item: any) => {
            if (isInit) {
              return subjectData.countNumData ? subjectData.countNumData[item] : item;
            }
          });
          return arr;
        }
        return subjectData.countNumData ? subjectData.countNumData[subjectData.defaultValue] : subjectData.defaultValue;
      }
    }
    return subjectData.defaultValue;
  };
  /**
   * 初始化复合组件
   */
  private formGroupInit = async (data: ISubjectV) => {
    return {
      groupItem: await this.formGroupValue(data.subjectData),
    };
  };

  /**
   * 取消大模块报红
   */
  private cancelBigModule = async (state: ISurvey, field: string) => {
    return state.gaugeData[state.menuIndex].isError.filter(
      // tslint:disable-next-line:no-any
      (item: any) => item.field !== field
    );
  };
  /**
   * 判断医生有没有资格填写
   * @param sje
   * @param visitNumber
   * @return boolean
   */
  private doctorVisitIf = (sje: ISubjectV, visitNumber: number) => {
    // 则visitNumbers里面有-1 则里面的代表排除的 如果没有 则代表包含的
    if (!sje.roleTypes || sje.roleTypes.includes('1')) {
      if (!sje.visitNumbers) {
        return true;
      }
      return sje.visitNumbers.includes('-1')
        ? !sje.visitNumbers.includes(visitNumber.toString())
        : sje.visitNumbers.includes(visitNumber.toString());
    }
    return false;
  };
  /**
   * 判断患者有没有资格填写
   * @param sje
   * @param visitNumber
   * @return boolean
   */
  private patientVisitIf = (sje: ISubjectV, visitNumber: number) => {
    // 则visitNumbers里面有-1 则里面的代表排除的 如果没有 则代表包含的
    if (!sje.roleTypes || sje.roleTypes.includes('0')) {
      if (!sje.visitNumbers) {
        return true;
      }
      return sje.visitNumbers.includes('-1')
        ? !sje.visitNumbers.includes(visitNumber.toString())
        : sje.visitNumbers.includes(visitNumber.toString());
    }

    return false;
  };

  /**
   * 返回第一个符合条件的下标,此方法只能适用于IAllGauge[];这个数据结构
   * @param arr 题目大模块
   */
  private single(arr: IAllGauge[]) {
    for (let i = 0; i < arr.length; i++) {
      if (arr[i].isShow) {
        return i;
      }
    }
  }

  /**
   * 获取问卷详情
   * @param id 问卷id
   */
  private getGaugeDetail = async (id: string) => {
    const { result } = await GaugeSurvey.getGaugeDetail({ id });
    return result;
  };

  /**
   * 获取编辑问卷缓存
   *
   */
  private getCacheGauge = async (patientId: string, answerId: string) => {
    const { result } = await GaugeSurvey.getCacheGauge({ patientId, answerId });
    return result;
  };
  /**
   * 获取匿名问卷缓存
   */

  private getUnknownCacheGauge = async () => {
    const { result } = await GaugeSurvey.getUnknownCacheGauge();
    return result;
  };

  /**
   * 获取原题目
   */
  private getSurveyOriginal = async (data: { questionId?: string }) => {
    const { result } = await GaugeSurvey.getSurvey(data);
    return result;
  };
  /**
   * 获取扫码编辑问卷的信息，包括缓存信息
   */
  private getQrCodeAnswerOrCache = async (code: string) => {
    const { result } = await GaugeSurvey.getQrCodeAnswerOrCache({ code });
    return result;
  };
}
export default SurveyModelUtil;
